package com.a.l;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;

import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;

public class JwtUtil {
    //默认 3 分钟有效
    private static final long DEFAULT_TIMEOUT = 1000 * 60 * 60 * 24 * 30;

    public static String jwtHmac(String secret, Object... payloads) {
        return jwtHmacWithExp(secret, DEFAULT_TIMEOUT, payloads);
    }

    public static String jwtHmacWithExp(String secret, long expire, Object... payloads) {
        if (invalidParams(payloads)) {
            return null;
        }
        JWTCreator.Builder builder = JWT.create();
        for (int i = 0; i < payloads.length; i += 2) {
            String key = (String) payloads[i];
            Object val = payloads[i + 1];
            if (val == null) {
                builder.withClaim(key, (String) null);
            } else if (val instanceof String) {
                builder.withClaim(key, (String) val);
            } else if (val instanceof Boolean) {
                builder.withClaim(key, (Boolean) val);
            } else if (val instanceof Integer) {
                builder.withClaim(key, (Integer) val);
            } else if (val instanceof Long) {
                builder.withClaim(key, (Long) val);
            } else if (val instanceof Double) {
                builder.withClaim(key, (Double) val);
            } else if (val instanceof Date) {
                builder.withClaim(key, (Date) val);
            } else {
                throw new RuntimeException("invalid payload type");
            }
        }
        Calendar calendar = Calendar.getInstance();
        Calendar ex = Calendar.getInstance();
        ex.add(Calendar.DATE, 30);
        return builder
                .withIssuedAt(calendar.getTime())
                .withExpiresAt(ex.getTime())
                .sign(Algorithm.HMAC256(secret));
    }

    private static boolean invalidParams(Object... params) {
        return params == null || params.length % 2 != 0;
    }

    public static String getJWTToken(String ccuAuthCode, String ccuid, String appid, int pid, String clientId) {

        String token = JwtUtil.jwtHmac(
                ccuAuthCode,
                "appid", appid,
                "ccuid", ccuid,
                "pid", pid,
                "client_id", clientId,
                "expires_in", 60 * 60 * 24 * 30
        );
        return token;
    }

    public static void main(String[] args) {
        String ccuAuthCode = "EA6337D6"; //CCU_203743 的主机授权码
        String ccuid = "CCU_203743";
        String appid = "10001350";
        int pid = 10000;
        String client_id = "CT1640199194138";
        String token = JwtUtil.jwtHmac(
                ccuAuthCode,
                "appid", appid,
                "ccuid", ccuid,
                "pid", pid,
                "client_id", client_id,
                "expires_in", 60 * 60 * 24 * 30
        );
        System.out.println(token);
    }
}
